package com.example.denglu.controller;




import com.example.denglu.dto.UserExcelDto;
import com.example.denglu.dto.UserInformationExcelDto;
import com.example.denglu.entity.User;
import com.example.denglu.entity.UserInformation;
import com.example.denglu.service.UserInformationService;
import com.example.denglu.service.UserService;
import com.example.denglu.util.ExcelUtil;
import com.example.denglu.vo.UserVo;
import com.sun.xml.internal.ws.resources.UtilMessages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.List;

@Api(tags = "excel导出")
@RestController
@RequestMapping( "/excel")
public class ExcelController {
    @Autowired
    UserService userService;
    @Autowired
    UserInformationService userInformationService;


    /**
     * 工时Excel模板下载
     * @param response 响应
     * @param filName 表名
     */
    @ApiOperation(value = "下载事项主题模板", produces = "application/octet-stream")
    @RequestMapping(value = "/downloadTemplate", produces = "application/octet-stream", method = RequestMethod.GET)
    public void downloadExcelTemplate(HttpServletResponse response,String filName) throws IOException {
    // String filName="主题事项导入模板.xlsx";
     ExcelUtil.DownloadTemplate(response,filName);
    }


    /**
     * 导出excel用户表
     * @param
     */
    @ApiOperation(value = "表格导出" , produces = "application/octet-stream")
    @RequestMapping(value = "/downloadExcel" , produces = "application/octet-stream" ,method = RequestMethod.GET)
    public void  downloadExcel(HttpServletResponse response,HttpServletRequest request,String fileName , String tableType){
        if (tableType.equals("用户")) {
            List<UserExcelDto> lists = userService.Downloads();
            ExcelUtil.writerExcel(fileName,response,request,lists, UserExcelDto.class); }
        if(tableType.equals("信息")){
            List<UserInformationExcelDto> list = userInformationService.Download();
            ExcelUtil.writerExcel(fileName,response,request,list, UserInformationExcelDto.class);
        }

    }


    /**
     * 导入excel表
     */
    @ApiOperation(value = "excel导入", produces = "application/octet-stream")
    @RequestMapping(value = "/excelInPut", produces = "application/octet-stream", method = RequestMethod.GET)
    public void excelInPut(MultipartFile filter) {
        String filtername = filter.getOriginalFilename();
        if (filtername != "" && filtername.equals("账户信息.xlsx")) {
            Object objects = ExcelUtil.readExcel(filter, new UserExcelDto(), 1, 1);
            List<UserExcelDto> userExcelDtos = (List<UserExcelDto>) objects;
            for (UserExcelDto userExcelDto : userExcelDtos) {
                UserVo userVo = UserVo.builder()
                        .username(userExcelDto.getUsername())
                        .password(userExcelDto.getPassword())
                        .build();
                userService.regist(userVo);
            }
        }
        if (filtername != "" && filtername.equals("用户信息.xlsx")) {
            Object objects = ExcelUtil.readExcel(filter, new UserInformationExcelDto(), 1, 1);
            List<UserInformationExcelDto> userInformationExcelDtos = (List<UserInformationExcelDto>) objects;
            for (UserInformationExcelDto userInformationExcelDto : userInformationExcelDtos) {
                UserInformation userInformation = UserInformation.builder()
                        .name(userInformationExcelDto.getName())
                        .age(userInformationExcelDto.getAge())
                        .gender(userInformationExcelDto.getGender())
                        .position(userInformationExcelDto.getPosition())
                        .build();
                userInformationService.add(userInformation);
            }
        }

    }
}


